<template>
  <div>
    <van-nav-bar title="黑马头条-登录" fixed />
    <div class="top">
      <van-form @submit="onSubmit">
        <van-field
          v-model="user.mobile"
          name="mobile"
          label="手机号码:"
          required
          placeholder="请输入手机号码"
          :rules="[{ required: true, message: '请输入正确手机号',pattern:/^1[3-9]\d{9}$/, }]"
        />
        <van-field
          v-model="user.code"
          name="code"
          type="number"
          required
          label="验证码:"
          placeholder="请输入正确验证码"
          :rules="[{ required: true, message: '请输入正确验证码', pattern: /^\d{6}$/}]"
        />
        <div style="margin: 16px">
          <van-button round block type="info" :disabled="isLogin" native-type="submit" :loading="isLogin" loading-text="加载中..."
            >登录</van-button
          >
        </div>
      </van-form>
    </div>
    <h1>登录组件</h1>
  </div>
</template>

<script>
import { loginAPI } from '@/api'
import Notify from '@/ui/Notify'
export default {
  name: 'Login',
  data () {
    return {
      user: {
        mobile: '13900000000',
        code: '246810'
      },
      isLogin: false
    }
  },
  methods: {
    async onSubmit (values) {
      this.isLogin = true
      const [err, res] = await loginAPI(values)
      if (err) Notify({ type: 'danger', message: '登录失败' })
      else {
        Notify({ type: 'success', message: '登录成功' })
      }console.log(res)
      this.isLogin = false
    }
  }
}
</script>

<style lang="less" scoped>
.top {
  margin-top: 1.917rem;
}
</style>
